<template>
  <div>
    <el-drawer v-model="drawer" :title="drawerTitle" :with-header="true">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="140px"
        class="demo-ruleForm"
      >
        <el-form-item label="联系人姓名：" prop="contactName">
          <el-input v-model="ruleForm.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人职务：" prop="contactTitle">
          <el-input v-model="ruleForm.contactTitle" placeholder="请输入联系人职务" />
        </el-form-item>
        <el-form-item label="联系人电话：" prop="contactPhone">
          <el-input v-model="ruleForm.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="备注：" prop="contactRemark">
          <el-input v-model="ruleForm.contactRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="handleClose(ruleFormRef)">确定</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import type { FormInstance } from 'element-plus'
  import { ElMessage } from 'element-plus'
  import {
    postSupplierContact,
    putSupplierContact,
  } from '@/api/controller/supplier-contact-controller/index'
  const drawer = ref(false)
  //打开弹窗
  const drawerTitle = ref()
  const ifAdd = ref(true)
  const show = async (row, id) => {
    if (row.id) {
      ruleForm.value = { ...row }
      drawerTitle.value = '编辑联系人'
      ifAdd.value = false
    } else {
      drawerTitle.value = '新增联系人'
      ifAdd.value = true
      ruleForm.value = {
        supplierId: id,
        contactName: null,
        contactTitle: null,
        contactPhone: null,
        contactRemark: null,
      }
    }
    drawer.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }

  //提交的数据
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = ref({
    supplierId: null,
    contactName: null,
    contactTitle: null,
    contactPhone: null,
    contactRemark: null,
  })
  const rules = reactive({
    contactName: [
      { required: true, message: '请输入联系人姓名', trigger: 'blur' },
      { min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' },
    ],
    contactPhone: [
      { required: true, message: '请输入电话', trigger: 'blur' },
      {
        pattern: /^(1[3456789]\d{9}|0\d{2,3}-\d{7,8})$/,
        message: '请输入正确的电话格式',
        trigger: 'blur',
      },
    ],
    contactTitle: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
    contactRemark: [{ min: 0, max: 64, message: '长度不超过64个字符', trigger: 'blur' }],
  })
  //提交
  const handleClose = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        let result
        if (ifAdd.value) {
          result = await postSupplierContact(ruleForm.value)
        } else {
          result = await putSupplierContact(ruleForm.value)
        }
        if (result.code === 200) {
          await emit('getData')
          drawer.value = false
          ElMessage.success('更新成功')
        }
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }

  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>
